"""
@author: zlr
@file: sl_shoppingsystem.py
@time: 2023/3/24 19:05
"""


# 登录系统


# 登录
def login(username, passwd):
    if username == "zlr" and passwd == "123456":
        return True


# 商品界面
def goods_info():
    print("商品信息如下:")
    print("商品编号   商品名   单价")
    for i in goods:
        print(f' {i}        {goods[i]["name"]}     {goods[i]["price"]}')
    print("-" * 50)


# 购物并加入购物车
def shopping(goods_id, goods_num):
    cart_init[goods_id] = cart_init.get(goods_id, 0) + goods_num
    print(f"{goods[goods_id]['name']}已加入购物车")
    print("-"*50)
    print("商品编号 商品数量")
    print(f"{goods_id}         {goods_num}")


# 查看购物车并结算
def cart_check():
    print("查看购物车")
    print(f"{'商品编号'} {'商品名'} {'单价'} {'数量'}")
    for i in cart_init:
        print(f'{i}      {goods[i]["name"]}   {goods[i]["price"]}     {cart_init[i]}')


def cart_count():
    global pay_cart
    moneylast = 100
    for i in cart_init:
        pay_cart += cart_init[i] * goods[i]["price"]
    if pay_cart <= 100:
        moneylast -= pay_cart
        print(f"已成功结算，本次消费金额为{pay_cart},您的余额为{moneylast}")

    else:
        print("您的余额不足")


# 退出
def exit():
    print(f"您总共消费了{pay_cart}元")
    print("谢谢惠顾！欢迎您下次光临")


# main
# user = {"root": {"passwd": '123456', "money": 100}}
print("欢迎进入购物系统")
goods = {"G1": {"name": 'Coco', "price": 5},
         "G2": {"name": 'Cofe', "price": 4},
         "G3": {"name": 'Coke', "price": 4},
         "G4": {"name": 'Milk', "price": 5}
         }
# 购物车
cart_init = {}
pay_cart = 0
money_last = 100

# 选择
while 1:
    print("1、登录")
    print("2、退出")
    option1 = input("请输入您的选择:")
    if option1 == '1':
        username = input("请输入用户名:")
        password = input("请输入密码:")
        if login(username, password):
            print("登录成功")
            while 2:
                print("请输入您的选择:")
                print("1、查看商品信息")
                print("2、选择商品并加入购物车")
                print("3、查看并结算")
                print("4、退出")
                option2 = input("您的选择是:")
                if option2 == '1':
                    goods_info()
                elif option2 == '2':
                    goods_id = input("您想加购的商品编号为:")
                    goods_num = int(input("加购数量为:"))
                    shopping(goods_id, goods_num)
                elif option2 == '3':
                    cart_check()
                    cart_count()
                elif option2 == '4':
                    print("退出")
                    exit()
                    break
                else:
                    print("输入错误")
        elif option1 == '2':
            print("退出")
            break
        else:
            print("输入错误")